﻿using EyFramework.ORM;
using Ncov2019.Entity;
using Ncov2019.Entity.Dto;
using SqlSugar;
using System.Collections.Generic;

namespace Ncov2019.Core.Dao
{
    /// <summary>
    ///  NCOV_ORDER_RECORD表的数据库操作
    /// </summary>
    public class NcovOrderRecordDao : BaseDaoHis<NcovOrderRecord>, INcovOrderRecordDao
    {
        public bool ClearOrders(long record_seq)
        {
            return Db.Deleteable<NcovOrderRecord>().Where(p => p.RECORD_SEQ == record_seq).ExecuteCommand() >= 0;
        }

        public bool DeleteOrders(List<NcovOrderRecordDto> orders)
        {
            return Db.Deleteable<NcovOrderRecordDto>().Where(orders).ExecuteCommand() >= 0;
        }

        public bool LisOrder(LisOrderProParam orderParam)
        {
            var prm_JIUZHENHAO = new SugarParameter(":prm_JIUZHENHAO", orderParam.PRM_JIUZHENHAO);
            var prm_SHENFENZH = new SugarParameter(":prm_SHENFENZH", orderParam.PRM_SHENFENZH);
            var prm_JIUZHENLX = new SugarParameter(":prm_JIUZHENLX", orderParam.PRM_JIUZHENLX);

            var prm_XINGMING = new SugarParameter(":prm_XINGMING", orderParam.PRM_XINGMING);
            var prm_XINGBIE = new SugarParameter(":prm_XINGBIE", orderParam.PRM_XINGBIE == "女" ? 2 : 1);
            var prm_CHUSHENGRQ = new SugarParameter(":prm_CHUSHENGRQ", orderParam.PRM_CHUSHENGRQ);
            var prm_DIANHUAHM = new SugarParameter(":prm_DIANHUAHM", orderParam.PRM_DIANHUAHM);
            var prm_ZHENDUANMC = new SugarParameter(":prm_ZHENDUANMC", orderParam.PRM_ZHENDUANMC);
            var prm_KAIDANKSID = new SugarParameter(":prm_KAIDANKSID", orderParam.PRM_KAIDANKSID);
            var prm_KAIDANKSMC = new SugarParameter(":prm_KAIDANKSMC", orderParam.PRM_KAIDANKSMC);
            var prm_KAIDANYSID = new SugarParameter(":prm_KAIDANYSID", orderParam.PRM_KAIDANYSID);
            var prm_KAIDANYSXM = new SugarParameter(":prm_KAIDANYSXM", orderParam.PRM_KAIDANYSXM);
            var prm_KAIDANSJ = new SugarParameter(":prm_KAIDANSJ", orderParam.PRM_KAIDANSJ);
            var prm_JIANYANXMDM = new SugarParameter(":prm_JIANYANXMDM", orderParam.PRM_JIANYANXMDM);
            var prm_JIANYANXMMC = new SugarParameter(":prm_JIANYANXMMC", orderParam.PRM_JIANYANXMMC);
            var prm_DAYINRID = new SugarParameter(":prm_DAYINRID", orderParam.PRM_DAYINRID);
            var prm_DAYINRXM = new SugarParameter(":prm_DAYINRXM", orderParam.PRM_DAYINRXM);
            var prm_DAYINSJ = new SugarParameter(":prm_DAYINSJ", orderParam.PRM_DAYINSJ);
            var prm_TIAOMAHAO = new SugarParameter(":prm_TIAOMAHAO", null, true);
            var prm_AppCode = new SugarParameter(":prm_AppCode", null, true);
            var prm_DataBuffer = new SugarParameter(":prm_DataBuffer", null, true);
            var reslt = Db.Ado.UseStoredProcedure().GetDataTable("LIMS6XS.PKG_JY_XIAOSHANEY.Prc_JY_BIAOBENXX",
                prm_JIUZHENHAO,
                prm_SHENFENZH,
                prm_JIUZHENLX,
                prm_XINGMING,
                prm_XINGBIE,
                prm_CHUSHENGRQ,
                prm_DIANHUAHM,
                prm_ZHENDUANMC,
                prm_KAIDANKSID,
                prm_KAIDANKSMC,
                prm_KAIDANYSID,
                prm_KAIDANYSXM,
                prm_KAIDANSJ,
                prm_JIANYANXMDM,
                prm_JIANYANXMMC,
                prm_DAYINRID,
                prm_DAYINRXM,
                prm_DAYINSJ,
                prm_TIAOMAHAO,
                prm_AppCode,
                prm_DataBuffer);
            orderParam.PRM_TIAOMAHAO = prm_TIAOMAHAO.Value.ToString();
            orderParam.PRM_APPCODE = prm_AppCode.Value.ToString();
            orderParam.PRM_DATABUFFER = prm_DataBuffer.Value.ToString();
            return true;
        }

        public List<NcovOrderRecord> QueryDataBy(NcovRecordParam param)
        {
            string whereStr = string.Format("t.ORD_DT BETWEEN TO_DATE('{0} 00:00:00','YYYY-MM-DD HH24:MI:SS') and TO_DATE('{1} 23:59:59','YYYY-MM-DD HH24:MI:SS') and t.department='{2}'", param.START_DT, param.END_DT, param.department);
            return Db.Queryable<NcovOrderRecord>("t").Where(whereStr).OrderBy("ORDER_SEQ").ToList();
        }

        public List<NcovOrderRecordDto> QueryDataByRecSeq(long detailDataSeq)
        {
            return Db.Queryable<NcovOrderRecordDto>().Where(p => p.RECORD_SEQ == detailDataSeq).ToList();
        }

        public List<long> QueryHasImport(long record_seq)
        {
            return Db.Queryable<NcovOrderRecord>().Where(p => p.RECORD_SEQ == record_seq).Select(p => p.PAT_SEQ).ToList();
        }

        public List<GyBingrenxxDto> SearchBrxx(string sInput)
        {
            string sql = @"select a.xingming,a.shenfenzh,a.xingbie,a.jiatingdh,a.jiatingdz,a.chushengrq from his6.gy_bingrenxx a where " + sInput + " and rownum=1 order by a.xiugaisj desc";
            return Db.Ado.SqlQuery<GyBingrenxxDto>(sql);
        }

        /// <summary>
        /// 根据身份证号码获取最后一次核酸条码
        /// </summary>
        /// <param name="shenFenZh"></param>
        /// <returns></returns>
        public NcovOrderRecord QueryLastByIdNo(string shenFenZh)
        {
            string sql = "SELECT * FROM NCOV_ORDER_RECORD WHERE ORDER_SEQ =(SELECT MAX (ORDER_SEQ) FROM NCOV_ORDER_RECORD A WHERE ID_NO = :ID_NO AND ORD_DT > SYSDATE - 7 and exists(select 1 from lims6xs.lcb_biaobenxx where bingrenid=A.CHT_ID AND TIAOMAHAO=A.BAR_NO AND JIANCESJ IS NULL))";
            return Db.Ado.SqlQuerySingle<NcovOrderRecord>(sql, new { ID_NO = shenFenZh });
        }

        /// <summary>
        /// 判断LIS是否获取到申请单信息
        /// </summary>
        /// <param name="jiuzhenid"></param>
        /// <param name="bingrenid"></param>
        /// <returns></returns>
        public bool LisHasReady(string jiuzhenid, string bingrenid)
        {
            string sql = "SELECT 1 FROM LIMS6XS.LCB_JIANYANSQD WHERE BINGRENID=:BINGRENID AND JIUZHENID=:JIUZHENID";
            return Db.Ado.SqlQuery<int>(sql, new { BINGRENID = bingrenid, JIUZHENID = jiuzhenid }).Count > 0;
        }
    }
}